﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Admin.CYT369.Com.WebBase;

namespace admin.cyt369.com.Controllers
{
    public partial class SystemsController
    {
        #region AppPlugs
        [HttpGet]
        public ActionResult GetAppPlugs()
        {
            int page = Request.Query.GetInteger("page", 0);
            int rows = Request.Query.GetInteger("rows", 20);
            int total = 0;
            string plugName = Request.Query.GetString("PlugName");

            var items = Admin.CYT369.Com.DAL.Services.AppService.Instance.GetAppPlugs(page, rows, ref total, plugName);

            return DefaultJson(new { total = total, rows = items });
        }

        [HttpGet]
        public IActionResult GetAppPlugsByID(int id)
        {
            var role = Admin.CYT369.Com.DAL.Services.AppService.Instance.GetAppPlugsByID(id);

            return DefaultJson(role);
        }

        [HttpDelete]
        public IActionResult DeleteAppPlugs(int id)
        {
            var resp = new Admin.CYT369.Com.WebBase.ExecuteResult();
            if (Admin.CYT369.Com.DAL.Services.AppService.Instance.DeleteAppPlugs(id))
            {
                resp.Result = true;
            }
            else
            {
                resp.Msg = "删除失败";
            }

            return DefaultJson(resp);
        }

        [HttpPost]
        public IActionResult SaveAppPlugs([FromForm]Admin.CYT369.Com.DAL.Domains.Apps.AppPlugs appPlugs)
        {
            var resp = new Admin.CYT369.Com.WebBase.ExecuteResult();

            if (appPlugs != null && Admin.CYT369.Com.DAL.Services.AppService.Instance.SaveAppPlugs(appPlugs))
            {
                resp.Result = true;
            }
            else
            {
                resp.Msg = "保存失败";
            }

            return DefaultJson(resp);
        }
        #endregion

        #region Role
        [HttpGet]
        public IActionResult GetRoles()
        {
            int page = Request.Query.GetInteger("page", 0);
            int rows = Request.Query.GetInteger("rows", 20);
            int total = 0;

            var name = Request.Query.GetString("Name");

            var items = Admin.CYT369.Com.DAL.Services.UserService.Instance.GetRoles(page, rows, ref total, name);

            return DefaultJson(new { total = total, rows = items });
        }

        [HttpGet]
        public IActionResult GetRoleByID(int id)
        {
            var role = Admin.CYT369.Com.DAL.Services.UserService.Instance.GetRoleByID(id);

            return DefaultJson(role);
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DeleteRole(int id)
        {
            var resp = new Admin.CYT369.Com.WebBase.ExecuteResult();
            if (Admin.CYT369.Com.DAL.Services.UserService.Instance.DeleteRole(id))
            {
                resp.Result = true;
            }
            else
            {
                resp.Msg = "删除失败";
            }

            return DefaultJson(resp);
        }

        [HttpPost]
        public IActionResult SaveRole([FromForm] Admin.CYT369.Com.DAL.Domains.Role role)
        {
            var resp = new Admin.CYT369.Com.WebBase.ExecuteResult();

            if (role != null && Admin.CYT369.Com.DAL.Services.UserService.Instance.SaveRole(role))
            {
                resp.Result = true;
            }
            else
            {
                resp.Msg = "保存失败";
            }

            return DefaultJson(resp);
        }
        #endregion

        #region UserRoles
        [HttpGet]
        public IActionResult GetRoleUsersByRoleID(int id)
        {
            int page = Request.Query.GetInteger("page", 0);
            int rows = Request.Query.GetInteger("rows", 20);
            int total = 0;

            var items = Admin.CYT369.Com.DAL.Services.UserService.Instance.GetRoleUsersByRoleID(page, rows, ref total, id);

            return DefaultJson(new { total = total, rows = items });
        }

        [HttpGet]
        public IActionResult GetRoleUsersByUserID(int id)
        {
            int page = Request.Query.GetInteger("page", 0);
            int rows = Request.Query.GetInteger("rows", 20);
            int total = 0;

            var items = Admin.CYT369.Com.DAL.Services.UserService.Instance.GetRoleUsersByUserID(page, rows, ref total, id);

            return DefaultJson(new { total = total, rows = items });
        }

        /// <summary>
        /// 保存用户与角色的关系
        /// </summary>
        /// <param name="items"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult SaveRoleUser([FromBody] List<Admin.CYT369.Com.DAL.Domains.sys.UserRole> items)
        {
            var resp = new Admin.CYT369.Com.WebBase.ExecuteResult();

            if (items != null && Admin.CYT369.Com.DAL.Services.UserService.Instance.SaveRoleUser(items))
            {
                resp.Result = true;
            }
            else
            {
                resp.Msg = "保存失败";
            }

            return DefaultJson(resp);
        }

        /// <summary>
        /// 删除用户与角色的关系
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DeleteRoleUser(int id)
        {
            var resp = new Admin.CYT369.Com.WebBase.ExecuteResult();
            if (Admin.CYT369.Com.DAL.Services.UserService.Instance.DeleteRoleUser(id))
            {
                resp.Result = true;
            }
            else
            {
                resp.Msg = "删除失败";
            }

            return DefaultJson(resp);
        }

        /// <summary>
        /// 删除用户与角色的关系
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DeleteRoleUserList([FromBody] List<int> ids)
        {
            var resp = new Admin.CYT369.Com.WebBase.ExecuteResult();
            if (Admin.CYT369.Com.DAL.Services.UserService.Instance.DeleteRoleUserList(ids))
            {
                resp.Result = true;
            }
            else
            {
                resp.Msg = "删除失败";
            }

            return DefaultJson(resp);
        }
        #endregion

        #region RoleMenus
        /// <summary>
        /// 菜单功能树
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetAppMenuTree()
        {
            var menus = Admin.CYT369.Com.DAL.Services.UserService.Instance.GetAppMenuTree();
            return DefaultJson(menus);
        }


        [HttpGet]
        public IActionResult GetRoleMenusByRoleID(int id)
        {
            var menus = Admin.CYT369.Com.DAL.Services.UserService.Instance.GetRoleMenusByRoleID(id);
            return DefaultJson(menus);
        }

        /// <summary>
        /// 按菜单ID获取对应的权限操作列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetAppMenuOperations(int id)
        {
            var items = Admin.CYT369.Com.DAL.Services.UserService.Instance.GetAppMenuOperations(id);
            return DefaultJson(items);
        }

        /// <summary>
        /// 获取角色-菜单-操作三者的关联关系
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="menuId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetRoleMenuOperations(int roleId, int menuId)
        {
            var menus = Admin.CYT369.Com.DAL.Services.UserService.Instance.GetRoleMenuOperations(roleId, menuId);
            return DefaultJson(menus);
        }


        /// <summary>
        /// 更新角色菜单权限
        /// </summary>
        /// <param name="items"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdateRoleMenu([FromBody] List<Admin.CYT369.Com.DAL.Domains.Apps.RoleMenu> items)
        {
            var resp = new Admin.CYT369.Com.WebBase.ExecuteResult();
            if (items != null && Admin.CYT369.Com.DAL.Services.UserService.Instance.UpdateRoleMenu(items))
            {
                resp.Result = true;
            }
            else
            {
                resp.Msg = "保存失败";
            }

            return DefaultJson(resp);
        }

        [HttpPost]
        public ActionResult UpdateRoleMenuOperation()
        {
            int roleId = Request.Form.GetInteger("RoleID", 0);
            int menuId = Request.Form.GetInteger("MenuID", 0);
            int operationId = Request.Form.GetInteger("OperationID", 0);
            bool enable = Request.Form.GetBoolean("Enable", false);

            var resp = new Admin.CYT369.Com.WebBase.ExecuteResult();
            if (Admin.CYT369.Com.DAL.Services.UserService.Instance.UpdateRoleMenuOperation(roleId, menuId, operationId, enable))
            {
                resp.Result = true;
            }
            else
            {
                resp.Msg = "保存失败";
            }

            return DefaultJson(resp);
        }
        #endregion

        #region Users
        [HttpGet]
        public ActionResult GetUsers()
        {
            int page = Request.Query.GetInteger("page", 0);
            int rows = Request.Query.GetInteger("rows", 20);

            var departmentId = Request.Query.GetInteger("DepartmentID");
            var organizationId = Request.Query.GetInteger("OrganizationID");
            string fullName = Request.Query.GetString("FullName");
            string userName = Request.Query.GetString("UserName");
            var roleId = Request.Query.GetInteger("RoleID");
            var notInRoleId = Request.Query.GetInteger("NotInRoleID");
            int total = 0;

            var items = Admin.CYT369.Com.DAL.Services.UserService.Instance.GetUsers(page, rows, ref total, organizationId, departmentId, roleId, notInRoleId, fullName, userName);
            foreach (var item in items)
            {
                item.PasswordHash = string.Empty;
            }

            return DefaultJson(new { total = total, rows = items });
        }

        [HttpGet]
        public IActionResult GetUserByID(int id)
        {
            var user = Admin.CYT369.Com.DAL.Services.UserService.Instance.GetUserByID(id);
            if (user != null)
            {
                user.PasswordHash = string.Empty;
                var departmentUsers = Admin.CYT369.Com.DAL.Services.OrganizationService.Instance.GetUserDepartments(id);
                var positionUsers = Admin.CYT369.Com.DAL.Services.OrganizationService.Instance.GetUserPositions(id);
                return DefaultJson(new
                {
                    User = user,
                    DepartmentUsers = departmentUsers,
                    PositionUsers = positionUsers,
                });
            }
            return DefaultJson(user);
        }

        [HttpGet]
        public IActionResult GetEditUserByID(int id)
        {
            var user = Admin.CYT369.Com.DAL.Services.UserService.Instance.GetUserByID(id);
            if (user != null)
            {
                user.PasswordHash = string.Empty;
                return DefaultJson(new
                {
                    User = user,
                });
            }

            return Content(string.Empty);
        }

        [HttpPost]
        public IActionResult SaveUser([FromForm] Admin.CYT369.Com.DAL.Domains.SysUser user)
        {
            var resp = new Admin.CYT369.Com.WebBase.ExecuteResult();

            if (user != null)
            {
                try
                {
                    #region 准备数据
                    user.CreatedTime = DateTime.Now;
                    user.ChangedTime = DateTime.Now;
                    if (!string.IsNullOrEmpty(user.UserName))
                    {
                        user.UserName = user.UserName.Trim();
                        user.NormalizedUserName = user.UserName.ToUpper();
                    }

                    if (string.IsNullOrEmpty(user.FullNameCode))
                    {
                        if (!string.IsNullOrEmpty(user.FullName))
                        {
                            user.FullNameCode = Admin.CYT369.Com.Utility.HZPY.Get_Py_Letters(user.FullName);
                        }
                    }

                    if (user.Id <= 0)
                    {
                        user.IsInternal = false;
                        user.UniqueCode = Guid.NewGuid().ToString();
                        user.SecurityStamp = Guid.NewGuid().ToString().Replace("-", "").ToUpper();
                        user.ConcurrencyStamp = Guid.NewGuid().ToString();

                        if (!string.IsNullOrEmpty(user.PasswordHash))
                        {
                            var options = this.HttpContext.RequestServices.GetService(typeof(Microsoft.Extensions.Options.IOptions<Microsoft.AspNetCore.Identity.PasswordHasherOptions>)) as Microsoft.Extensions.Options.IOptions<Microsoft.AspNetCore.Identity.PasswordHasherOptions>;
                            if (options != null)
                            {
                                var passwordHasher = new Microsoft.AspNetCore.Identity.PasswordHasher<Admin.CYT369.Com.DAL.Domains.SysUser>(options);
                                user.PasswordHash = passwordHasher.HashPassword(user, user.PasswordHash);
                            }
                        }
                    }

                    #region department & position
                    var departmentUsers = new List<Admin.CYT369.Com.DAL.Domains.DepartmentUser>();
                    var mainDepartmentId = Request.Form.GetInteger("MainDepartmentID");
                    if (mainDepartmentId.HasValue)
                    {
                        departmentUsers.Add(new Admin.CYT369.Com.DAL.Domains.DepartmentUser()
                        {
                            UserID = user.Id,
                            DepartmentID = mainDepartmentId.Value,
                            IsMain = true
                        });
                    }
                    var subsidiaryDepartmentIds = Request.Form.GetIntegerList("SubsidiaryDepartmentID", new char[] { ',' });
                    foreach (var depId in subsidiaryDepartmentIds)
                    {
                        if (mainDepartmentId != depId)
                        {
                            departmentUsers.Add(new Admin.CYT369.Com.DAL.Domains.DepartmentUser()
                            {
                                UserID = user.Id,
                                DepartmentID = depId,
                                IsMain = false
                            });
                        }
                    }


                    var positionUsers = new List<Admin.CYT369.Com.DAL.Domains.PositionUser>();
                    var mainPositionId = Request.Form.GetInteger("MainPositionID");
                    if (mainPositionId.HasValue)
                    {
                        positionUsers.Add(new Admin.CYT369.Com.DAL.Domains.PositionUser()
                        {
                            UserID = user.Id,
                            PositionID = mainPositionId.Value,
                            IsMain = true
                        });
                    }
                    var subsidiaryPositionIds = Request.Form.GetIntegerList("SubsidiaryPositionID", new char[] { ',' });
                    foreach (var posId in subsidiaryPositionIds)
                    {
                        if (mainPositionId != posId)
                        {
                            positionUsers.Add(new Admin.CYT369.Com.DAL.Domains.PositionUser()
                            {
                                UserID = user.Id,
                                PositionID = posId,
                                IsMain = false
                            });
                        }
                    }

                    #endregion
                    #endregion

                    lock (s_lock_object)
                    {
                        #region 用户查重
                        var existUser = Admin.CYT369.Com.DAL.Services.UserService.Instance.FindUserByName(user.NormalizedUserName);
                        if (existUser != null)
                        {
                            if (existUser.Id != user.Id)
                            {
                                throw new Admin.CYT369.Com.Utility.Exceptions.DataExistException(string.Format("登录账户名{0}已存在，请使用其它登录账户名", user.UserName));
                            }
                        }
                        #endregion

                        if (Admin.CYT369.Com.DAL.Services.UserService.Instance.SaveUser(user, departmentUsers, positionUsers))
                        {
                            resp.Result = true;
                        }
                        else
                        {
                            resp.Msg = "保存失败";
                        }
                    }
                }
                catch (Admin.CYT369.Com.Utility.Exceptions.DataExistException ex)
                {
                    resp.Msg = ex.Message;
                }
            }
            else
            {
                resp.Msg = "用户信息为空";
            }

            return DefaultJson(resp);
        }

        [HttpDelete]
        public IActionResult DeleteUser(int id)
        {
            var resp = new Admin.CYT369.Com.WebBase.ExecuteResult();
            if (Admin.CYT369.Com.DAL.Services.UserService.Instance.DeleteUser(id))
            {
                resp.Result = true;
            }
            else
            {
                resp.Msg = "删除失败";
            }

            return DefaultJson(resp);
        }

        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="loginUser"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult ResetUserPassword([FromBody] Admin.CYT369.Com.WebBase.AccountLogin loginUser)
        {
            var resp = new Admin.CYT369.Com.WebBase.ExecuteResult();

            if (loginUser != null)
            {
                if (!string.IsNullOrEmpty(loginUser.Password))
                {
                    var user = Admin.CYT369.Com.DAL.Services.UserService.Instance.GetUserByID(loginUser.ID);
                    if (user != null)
                    {
                        var options = this.HttpContext.RequestServices.GetService(typeof(Microsoft.Extensions.Options.IOptions<Microsoft.AspNetCore.Identity.PasswordHasherOptions>)) as Microsoft.Extensions.Options.IOptions<Microsoft.AspNetCore.Identity.PasswordHasherOptions>;
                        if (options != null)
                        {
                            var passwordHasher = new Microsoft.AspNetCore.Identity.PasswordHasher<Admin.CYT369.Com.DAL.Domains.SysUser>(options);
                            user.PasswordHash = passwordHasher.HashPassword(user, loginUser.Password);
                        }

                        if (Admin.CYT369.Com.DAL.Services.UserService.Instance.ResetUserPassword(user))
                        {
                            resp.Result = true;
                            resp.Msg = "密码修改成功";
                        }
                        else
                        {
                            resp.Result = false;
                            resp.Msg = "密码修改失败";
                        }
                    }
                    else
                    {
                        resp.Result = false;
                        resp.Msg = "用户不存在";
                    }
                }
                else
                {
                    resp.Result = false;
                    resp.Msg = "密码不能为空";
                }
            }
            else
            {
                resp.Result = false;
                resp.Msg = "参数为空";
            }

            return DefaultJson(resp);
        }

        #region 树形结构管理
        #region 用户部门
        [HttpGet]
        public IActionResult GetUserDepartments(int userId)
        {
            IQueryable<Admin.CYT369.Com.DAL.Domains.Department> department = null;
            var departmentUsers = Admin.CYT369.Com.DAL.Services.OrganizationService.Instance.GetUserDepartments(userId, null);
            var ids = (from o in departmentUsers
                       select o.DepartmentID).ToList();
            department = Admin.CYT369.Com.DAL.Services.OrganizationService.Instance.GetDepartmentByIDs(ids);

            return DefaultJson(department);
        }
        #endregion

        #region 用户岗位
        [HttpGet]
        public IActionResult GetUserPositions(int userId)
        {
            IQueryable<Admin.CYT369.Com.DAL.Domains.Position> positions = null;
            var positionUsers = Admin.CYT369.Com.DAL.Services.OrganizationService.Instance.GetUserPositions(userId, null);
            var ids = (from o in positionUsers
                       select o.PositionID).ToList();
            positions = Admin.CYT369.Com.DAL.Services.OrganizationService.Instance.GetPositionByIDs(ids);

            return DefaultJson(positions);
        }
        #endregion

        public IActionResult GetOrgDeptTrees()
        {
            bool includeUser = Request.Query.GetBoolean("IncludeUser", false);
            var organizations = Admin.CYT369.Com.DAL.Services.OrganizationService.Instance.GetOrganizations().ToArray();
            var departments = Admin.CYT369.Com.DAL.Services.OrganizationService.Instance.GetDepartments().ToArray();
            var list = _MakeOrganizationsTree(organizations, departments, 0, includeUser);

            return DefaultJson(list);
        }

        private List<admin.cyt369.com.Models.Orgs.OrgDepartmentUserTree> _MakeOrganizationsTree(IEnumerable<Admin.CYT369.Com.DAL.Domains.Organization> organizations,
                                                                                                          IEnumerable<Admin.CYT369.Com.DAL.Domains.Department> departments,
                                                                                                          int parentOrgId, bool includeUser)
        {
            var list = new List<admin.cyt369.com.Models.Orgs.OrgDepartmentUserTree>();

            foreach (var org in organizations.Where(c => c.ParentID == parentOrgId))
            {
                var item = new admin.cyt369.com.Models.Orgs.OrgDepartmentUserTree(org);
                item.iconCls = "fa fa-support";
                list.Add(item);

                item.children = _MakeDepartmentsTree(departments, 0, org.Id, includeUser);
                item.children.AddRange(_MakeOrganizationsTree(organizations, departments, org.Id, includeUser));

                if (includeUser)
                {
                    _MakeUsersTree(item, org.Id, 0);
                }
                if (item.children.Count > 0)
                {
                    item.state = "open";
                }
            }

            return list;
        }

        private List<admin.cyt369.com.Models.Orgs.OrgDepartmentUserTree> _MakeDepartmentsTree(IEnumerable<Admin.CYT369.Com.DAL.Domains.Department> departments,
                                                                                                         int parentDeptId, int orgId, bool includeUser)
        {
            var list = new List<admin.cyt369.com.Models.Orgs.OrgDepartmentUserTree>();
            var depts = departments.Where(c => c.ParentID == parentDeptId && c.OrgID == orgId);

            foreach (var dept in depts)
            {
                var item = new admin.cyt369.com.Models.Orgs.OrgDepartmentUserTree(dept);
                list.Add(item);
                item.children = _MakeDepartmentsTree(departments, dept.Id, orgId, includeUser);
                if (includeUser)
                {
                    _MakeUsersTree(item, dept.OrgID, dept.Id);
                }
                if (item.children.Count > 0)
                {
                    item.state = "open";
                }
            }

            return list;
        }

        private void _MakeUsersTree(admin.cyt369.com.Models.Orgs.OrgDepartmentUserTree parent, int? organizationId = null, int? departmentId = null)
        {
            int n = 0;
            var users = Admin.CYT369.Com.DAL.Services.UserService.Instance.GetUsers(0, int.MaxValue, ref n, organizationId, departmentId);
            foreach (var user in users)
            {
                var item = new admin.cyt369.com.Models.Orgs.OrgDepartmentUserTree(user);
                parent.children.Add(item);
            }
        }
        #endregion

        #endregion

        #region Menus
        /// <summary>
        /// 获取主模块
        /// </summary>
        /// <returns></returns>
        public virtual IActionResult GetAppModels()
        {
            var menus = Admin.CYT369.Com.DAL.Services.AppService.Instance.GetMenus();

            return DefaultJson(menus);
        }

        [HttpGet]
        public virtual IActionResult GetAppMenuTreeByID(int id)
        {
            var not_id = Request.Query.GetInteger("NOT_ID");
            var rootMenu = Admin.CYT369.Com.DAL.Services.AppService.Instance.GetAppMenuByID(id);
            rootMenu.IconClassName = null;
            var root = new List<Admin.CYT369.Com.DAL.Domains.Apps.SystemMenu>();
            root.Add(rootMenu);

            var menus = Admin.CYT369.Com.DAL.Services.AppService.Instance.GetMenuTree(parentId: id, not_id: not_id, needIcon: false);
            rootMenu.SubMenus = menus;

            return DefaultJson(root);
        }

        [HttpGet]
        public virtual IActionResult GetModelAppMenus(int id)
        {
            var menus = Admin.CYT369.Com.DAL.Services.AppService.Instance.GetMenuTree(id);

            return DefaultJson(menus);
        }

        [HttpGet]
        public virtual IActionResult GetAppMenuByID(int id)
        {
            var menu = Admin.CYT369.Com.DAL.Services.AppService.Instance.GetAppMenuByID(id);

            return DefaultJson(menu);
        }

        [HttpDelete]
        public virtual IActionResult DeleteAppMenu(int id)
        {
            var resp = new Admin.CYT369.Com.WebBase.ExecuteResult();
            if (Admin.CYT369.Com.DAL.Services.AppService.Instance.DeleteAppMenu(id))
            {
                resp.Result = true;
            }
            else
            {
                resp.Msg = "删除失败";
            }

            return DefaultJson(resp);
        }

        [HttpPost]
        public virtual IActionResult SaveAppMenu([FromForm] Admin.CYT369.Com.DAL.Domains.Apps.SystemMenu menu)
        {
            var resp = new Admin.CYT369.Com.WebBase.ExecuteResult();
            if (menu != null && Admin.CYT369.Com.DAL.Services.AppService.Instance.SaveAppMenu(menu))
            {
                resp.Result = true;
                resp.Msg = System.Text.Json.JsonSerializer.Serialize(menu);
            }
            else
            {
                resp.Msg = "保存失败";
            }

            return DefaultJson(resp);
        }
        #endregion

        #region AppOperations
        [HttpGet]
        public ActionResult GetAppOperations()
        {
            int page = Request.Query.GetInteger("page", 0);
            int rows = Request.Query.GetInteger("rows", 20);
            int total = 0;

            var items = Admin.CYT369.Com.DAL.Services.AppService.Instance.GetAppOperations(page, rows, ref total);

            return DefaultJson(new { total = total, rows = items });
        }

        [HttpGet]
        public ActionResult GetAppOperationByID(int id)
        {
            var items = Admin.CYT369.Com.DAL.Services.AppService.Instance.GetAppOperationByID(id);

            return DefaultJson(items);
        }

        [HttpDelete]
        public virtual IActionResult DeleteAppOperation(int id)
        {
            var resp = new Admin.CYT369.Com.WebBase.ExecuteResult();
            if (Admin.CYT369.Com.DAL.Services.AppService.Instance.DeleteAppOperation(id))
            {
                resp.Result = true;
            }
            else
            {
                resp.Msg = "删除失败";
            }

            return DefaultJson(resp);
        }

        [HttpPost]
        public virtual IActionResult SaveAppOperation([FromForm] Admin.CYT369.Com.DAL.Domains.Apps.AppOperation operation)
        {
            var resp = new Admin.CYT369.Com.WebBase.ExecuteResult();

            if (operation != null && Admin.CYT369.Com.DAL.Services.AppService.Instance.SaveAppOperation(operation))
            {
                resp.Result = true;
            }
            else
            {
                resp.Msg = "保存失败";
            }

            return DefaultJson(resp);
        }
        #endregion

        #region SystemMenuOperation
        [HttpGet]
        public ActionResult GetSystemMenuOperations(int id)
        {
            var items = Admin.CYT369.Com.DAL.Services.AppService.Instance.GetSystemMenuOperations(id);

            return DefaultJson(items);
        }

        [HttpPost]
        public virtual IActionResult SaveSystemMenuOperation([FromForm] Admin.CYT369.Com.DAL.Domains.Apps.SystemMenuOperation item)
        {
            var resp = new Admin.CYT369.Com.WebBase.ExecuteResult();

            if (item != null && Admin.CYT369.Com.DAL.Services.AppService.Instance.SaveSystemMenuOperation(item))
            {
                resp.Result = true;
            }
            else
            {
                resp.Msg = "保存失败";
            }

            return DefaultJson(resp);
        }

        [HttpPost]
        public virtual IActionResult SaveAllSystemMenuOperation([FromBody] List<Admin.CYT369.Com.DAL.Domains.Apps.SystemMenuOperation> items)
        {
            var resp = new Admin.CYT369.Com.WebBase.ExecuteResult();

            if (items != null && Admin.CYT369.Com.DAL.Services.AppService.Instance.SaveSystemMenuOperationList(items))
            {
                resp.Result = true;
            }
            else
            {
                resp.Msg = "保存失败";
            }

            return DefaultJson(resp);
        }
        #endregion

        #region Config
        [HttpGet]
        public virtual IActionResult GetAppDictionary(string id)
        {
            var items = Admin.CYT369.Com.DAL.Services.ConfigService.Instance.GetAppDictionarys(id);
            return DefaultJson(items);
        }

        [HttpGet]
        public virtual IActionResult GetAppDictionarys(string typeCode)
        {
            var items = Admin.CYT369.Com.DAL.Services.ConfigService.Instance.GetAppDictionarys(typeCode);
            return DefaultJson(items);
        }

        [HttpGet]
        public virtual IActionResult GetAppDictionaryByID(int id)
        {
            var item = Admin.CYT369.Com.DAL.Services.ConfigService.Instance.GetAppDictionaryByID(id);
            return DefaultJson(item);
        }

        [HttpGet]
        public virtual IActionResult GetAppDictionaryByTID(string typeCode, int id)
        {
            var item = Admin.CYT369.Com.DAL.Services.ConfigService.Instance.GetAppDictionaryByTID(typeCode, id);
            return DefaultJson(item);
        }

        [HttpDelete]
        public virtual IActionResult DeleteAppDictionary(int id)
        {
            var resp = new Admin.CYT369.Com.WebBase.ExecuteResult();
            if (Admin.CYT369.Com.DAL.Services.ConfigService.Instance.DeleteAppDictionary(id))
            {
                resp.Result = true;
            }
            else
            {
                resp.Msg = "删除失败";
            }

            return DefaultJson(resp);
        }

        [HttpPost]
        public virtual IActionResult SaveAppDictionary([FromForm] Admin.CYT369.Com.DAL.Domains.Apps.AppDictionary appDictionary)
        {
            var resp = new Admin.CYT369.Com.WebBase.ExecuteResult();

            if (appDictionary != null && Admin.CYT369.Com.DAL.Services.ConfigService.Instance.SaveAppDictionary(appDictionary))
            {
                resp.Result = true;
            }
            else
            {
                resp.Msg = "保存失败";
            }

            return DefaultJson(resp);
        }

        #endregion

        #region 系统日志
        public JsonResult GetSystemLogs()
        {
            int page = Request.Query.GetInteger("page", 0);
            int rows = Request.Query.GetInteger("rows", 20);

            int count = 0;

            var items = Admin.CYT369.Com.DAL.Services.AppService.Instance.GetAppLogs(page, rows, ref count);

            return DefaultJson(new { total = count, rows = items });
        }

        [HttpPost]
        public IActionResult SaveSystemLog([FromForm] Admin.CYT369.Com.DAL.Domains.Apps.AppLog appLog)
        {
            var resp = new Admin.CYT369.Com.WebBase.ExecuteResult();

            if (appLog != null)
            {
                if (Admin.CYT369.Com.DAL.Services.AppService.Instance.SaveAppLog(appLog))
                {
                    resp.Result = true;
                }
                else
                {
                    resp.Msg = "保存失败";
                }
            }
            else
            {
                resp.Msg = "保存失败";
            }

            return DefaultJson(resp);
        }
        #endregion
    }
}
